const mysql = require('mysql2/promise');
require('dotenv').config();

// 数据库配置
const dbConfig = {
  host: process.env.DB_HOST || 'localhost',
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || 'root',
  database: process.env.DB_NAME || 'community_service_platform',
  port: process.env.DB_PORT || 3306
};

// 测试数据库连接和查询
async function testDbConnection() {
  console.log('开始测试数据库连接...');
  console.log('数据库配置:', {
    host: dbConfig.host,
    user: dbConfig.user,
    password: '*****', // 隐藏密码
    database: dbConfig.database,
    port: dbConfig.port
  });

  try {
    // 创建连接
    const connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 查询skills表
    console.log('开始查询skills表...');
    const [rows] = await connection.execute('SELECT * FROM skills WHERE skill_id = 1');
    console.log('查询结果:', rows[0]);

    // 关闭连接
    await connection.end();
    console.log('✅ 数据库连接已关闭');
  } catch (error) {
    console.error('❌ 数据库操作失败:', error);
    console.error('错误堆栈:', error.stack);
  }
}

testDbConnection();